import 'package:flutter/cupertino.dart';
import 'package:s_k_todo/data/dio_static.dart';

import '../data/base_data.dart';
import '../model/user_model.dart';

class UserProvider extends ChangeNotifier {
  final UserApi _userApi = UserApi(DioStatic.getInstance().dio);
  String _phone = "";
  String _password = "";
  bool canLogin = false;
  UserData? _user;

  UserData? get user => _user;

  Future<BaseData<TokenData>> login() async {
    BaseData<TokenData> tokenData = await _userApi.login(_phone, _password);
    if(tokenData.code == 0){
      DioStatic.getInstance().initToken(tokenData.data?.accessToken,tokenData.data?.refreshToken);
    }
    return tokenData;
  }

  void changePhone(String phone) {
    _phone = phone;
    checkLogin();
  }

  void changePassword(String password) {
    _password = password;
    checkLogin();
  }

  void checkLogin() {
    if (_phone.length == 11 && _password.length > 5) {
      canLogin = true;
    } else {
      canLogin = false;
    }
    notifyListeners();
  }
}
